package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.BBS;
import com.jiuwu.spring.eshop.respository.BBSRepository;
import com.jiuwu.spring.eshop.respository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/manage")
public class BBSController {

    @Autowired
    BBSRepository bbsRepository;

    @RequestMapping("/placard_manage")
    public String loadBBS(Model model){
        List<BBS> bbsList = bbsRepository.findAllByOrderByUpdateTimeDesc();
        model.addAttribute("bbsList",bbsList);
        return "/manage/placard_manage";
    }

    @RequestMapping("/placard_add")
    public String loadAddBBS(){
        return "/manage/placard_add";
    }

    @RequestMapping("/placard_add_deal")
    public String addBBS(@RequestParam("BBSTitle")String title,
                         @RequestParam("BBSContent")String content){
        BBS bbs = new BBS();
        bbs.setTitle(title);
        bbs.setContent(content);
        bbs.setUpdateTime(new java.sql.Date(new java.util.Date().getTime()));
        bbsRepository.save(bbs);
        return "redirect:/manage/index?id=4";
    }

    @RequestMapping("/placard_del_deal")
    public String delBBS(HttpServletRequest request ) {
        for (String delId : request.getParameterValues("delId")) {
            Integer id = Integer.valueOf(delId);
            bbsRepository.deleteById(id);
            System.out.println("delete id :" + id);
        }
        return "redirect:/manage/index?id=4";
    }


    //------------------------------------thymeleaf-------------------------------------

    /**
     * 获取所有公告信息
     */
    @GetMapping(value = {"/placards","/placards/{pageNum}"})
    public String getPlacards(@PathVariable(value = "pageNum",required = false)Integer pageNum,Model model) {
        pageNum = pageNum == null ? 0 : pageNum > 0 ? pageNum - 1 : 0;
        Page<BBS> placardPage = bbsRepository.findAll(PageRequest.of(pageNum, 5));
        model.addAttribute("placardPage",placardPage);

        return "placard";
    }

    @GetMapping("/placard")
    public String toPlacard(){
        return "placard/addPlacard";
    }

    @PostMapping("/placard")
    public String addBBS2(@RequestParam(value = "BBSTitle")String title,
                         @RequestParam("BBSContent")String content){
        BBS bbs = new BBS();
        bbs.setTitle(title);
        bbs.setContent(content);
        bbs.setUpdateTime(new java.sql.Date(new java.util.Date().getTime()));
        bbsRepository.save(bbs);
        return "redirect:/manage/placards";
    }

    @GetMapping("/delPlacard/{placardId}")
    public String delBBS2(@PathVariable("placardId")Integer id) {
        bbsRepository.deleteById(id);
        return "redirect:/manage/placards";
    }

    /**
     * 图片空间请求
     */
    @RequestMapping("/picture")
    public String toPicture(){
        return "picture";
    }
}
